package applet.attendee.unlocked.components.travel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

import data.CMSRes;
import data.account.attendee.travel.TravelData;

import applet.attendee.CMSAttendeeView;
import applet.misc.action.EnableActionListener;
import applet.misc.graphics.AttendeeBackgroundPanel;
import applet.misc.item.EnableItemListener;
import applet.misc.web.WebsiteActionListener;

public class CMSAttendeeTravelView extends AttendeeBackgroundPanel
{
	private static final long serialVersionUID = 6490614358660371039L;
	
	private CMSAttendeeView attendeeView;
	public CMSAttendeeView attendeeView() { return attendeeView; }

	private JPanel travelDisplay;
	private JPanel travelActiveDialog;
	private JPanel travelInactiveDialog;
	
	private JComboBox airlines;
	private JComboBox dates;
	private JComboBox times;
	private JButton saveButton;
	private JLabel cost;
	
	public CMSAttendeeTravelView(CMSAttendeeView attendeeView)
	{
		super(new GridBagLayout());
		this.attendeeView = attendeeView;
		setBorder(new EmptyBorder(25, 25, 25, 25));

		createComponents();
		JPanel travelDialog = new JPanel(new BorderLayout());
		travelDialog.setBorder(new BevelBorder(BevelBorder.RAISED));
		
			JPanel titlePane = new JPanel(new BorderLayout());
			titlePane.setBackground(CMSRes.Colors.get("Theme Color Medium"));
			titlePane.setBorder(new EmptyBorder(10, 10, 10, 10));
				JLabel title = new JLabel("Travel Plan:");
				title.setFont(title.getFont().deriveFont(Font.BOLD));
				titlePane.add(title, BorderLayout.WEST);
			travelDialog.add(titlePane, BorderLayout.NORTH);
				
				travelDisplay = new JPanel(new BorderLayout());
				travelDisplay.setBorder(new BevelBorder(BevelBorder.LOWERED));
				
				travelActiveDialog = defineActiveDialog();
				travelInactiveDialog = defineInactiveDialog();
				
				if(attendeeView.attendee().travel() != null)
				{
					loadTravelData(attendeeView().attendee().travel());
					travelDisplay.add(travelActiveDialog, BorderLayout.CENTER);
				}
				else
					travelDisplay.add(travelInactiveDialog, BorderLayout.CENTER);
		travelDialog.add(travelDisplay, BorderLayout.CENTER);
		travelDialog.add(getThirdPartyBar(), BorderLayout.SOUTH);
		
		add(travelDialog);
	}
	
	private void createComponents()
	{
		saveButton = new JButton("Save Travel Plan");
		saveButton.setFont(saveButton.getFont().deriveFont(Font.BOLD));
		saveButton.setPreferredSize(new Dimension(200, 30));
		saveButton.addActionListener(new SaveTravelListener());
		saveButton.addActionListener(new EnableActionListener(saveButton, false));
		saveButton.setEnabled(false);
		
		airlines = new JComboBox(new String[] { "Frontier", "Northwestern", "American Airlines", "UFOs United", "Levitation Industries" });
		airlines.setPreferredSize(new Dimension(150, 30));
		airlines.addItemListener(new EnableItemListener(saveButton, true));

		dates = new JComboBox(new String[] { "10 - 01 - 12", "10 - 02 - 12", "10 - 03 - 12",
											"10 - 04 - 12", "10 - 05 - 12", "10 - 06 - 12",
											"10 - 07 - 12", "10 - 08 - 12" });
		dates.setPreferredSize(new Dimension(150, 30));
		dates.addItemListener(new EnableItemListener(saveButton, true));
		
		times = new JComboBox(new String[] { 	"9:00 AM", "9:50 AM", "10:15 AM",
													"11:45 AM", "1:00 PM", "2:20 PM",
													"4:30 PM" });
		times.setPreferredSize(new Dimension(150, 30));
		times.addItemListener(new EnableItemListener(saveButton, true));
		
		cost = new JLabel("Cost: $500.00");
	}
	
	private JPanel defineActiveDialog()
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setPreferredSize(new Dimension(450, 250));
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Airline:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);

		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(airlines, BorderLayout.WEST);

		panel.add(layoutHelper);

			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Arrival Date:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);
	
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(dates, BorderLayout.WEST);
	
		panel.add(layoutHelper);

			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Arrival Time:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);
	
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(times, BorderLayout.WEST);
	
		panel.add(layoutHelper);
		
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
			layoutHelper.add(cost, BorderLayout.WEST);
	
		panel.add(layoutHelper);

			layoutHelper = new JPanel(new GridBagLayout());
			layoutHelper.add(saveButton);

		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new GridBagLayout());
				JButton deleteButton = new JButton("Delete Travel Plan");
				deleteButton.setPreferredSize(new Dimension(200, 30));
				deleteButton.setFont(deleteButton.getFont().deriveFont(Font.BOLD));
				deleteButton.addActionListener(new DeleteTravelListener());
			layoutHelper.add(deleteButton);

		panel.add(layoutHelper);
		
		return panel;
	}
	
	private JPanel defineInactiveDialog()
	{
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setPreferredSize(new Dimension(450, 250));
		
			JButton createTravel = new JButton("Create Travel Plan");
			createTravel.setPreferredSize(new Dimension(200, 30));
			createTravel.setFont(createTravel.getFont().deriveFont(Font.BOLD));
			createTravel.addActionListener(new CreateTravelListener());
			createTravel.addActionListener(new EnableActionListener(saveButton, true));
			
		panel.add(createTravel);
		return panel;
	}
	
	private JPanel getThirdPartyBar()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBackground(CMSRes.Colors.get("Theme Color Medium"));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
			JButton thirdPartyButton = new JButton("Book Flights Online");
			thirdPartyButton.setPreferredSize(new Dimension(200, 30));
			thirdPartyButton.addActionListener(new WebsiteActionListener("http://www.expedia.com/Flights"));
		panel.add(thirdPartyButton, BorderLayout.EAST);
		return panel;
	}
	
	private void loadTravelData(TravelData data)
	{
		if(data != null)
		{
			for(int i = 0; i < airlines.getItemCount(); i++)
				if(airlines.getItemAt(i).equals(data.airline()))
					airlines.setSelectedIndex(i);

			for(int i = 0; i < dates.getItemCount(); i++)
				if(dates.getItemAt(i).equals(data.arrivalDate()))
					dates.setSelectedIndex(i);

			for(int i = 0; i < times.getItemCount(); i++)
				if(times.getItemAt(i).equals(data.arrivalTime()))
					times.setSelectedIndex(i);
			
			cost.setText("Cost: $" + data.cost() + ".00");
		}
	}
	
	private TravelData createTravelData()
	{
		String airline = (String) airlines.getSelectedItem();
		String aDate = (String) dates.getSelectedItem();
		String aTime = (String) times.getSelectedItem();
		int cost = 500;
		
		return new TravelData(airline, aDate, aTime, cost);
	}
	
	private class CreateTravelListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			loadTravelData(attendeeView().attendee().travel());
			travelDisplay.remove(travelInactiveDialog);
			travelDisplay.add(travelActiveDialog, BorderLayout.CENTER);
			travelDisplay.validate();
			travelDisplay.repaint();
		}
	}
	
	private class DeleteTravelListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			attendeeView.attendee().setTravel(null);
			attendeeView.attendee().saveData();
			
			travelDisplay.remove(travelActiveDialog);
			travelDisplay.add(travelInactiveDialog, BorderLayout.CENTER);
			travelDisplay.validate();
			travelDisplay.repaint();
		}
	}
	
	private class SaveTravelListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			attendeeView().attendee().setTravel(createTravelData());
			attendeeView().attendee().saveData();
		}
	}
}